/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src;
import static java.awt.Color.black;
import static java.awt.Color.red;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
/**
 *Για έλεγχο στήλης JTable με θετικό ακέραιο 
 * 
 */
public class PositiveIntDefaultCellEditor extends DefaultCellEditor{
   JTextField textField;
   JLabel label;
   JTable jt;
   int index;
    public PositiveIntDefaultCellEditor(JTextField jt, JLabel jLabel1, JTable jTable1, int index) {
          super(jt);
        this.textField = (JTextField)super.getComponent();
        this.label = jLabel1;
        this.jt = jTable1;
        this.index = index;
    }

    @Override
    public boolean stopCellEditing() {
         if (jt.getEditingColumn() != this.index )
             return  super.stopCellEditing();
                    
        int value;
        try {
            value = Integer.parseInt(this.textField.getText());
        } catch (Exception e) {
             //super.stopCellEditing();
             this.textField.setBorder(BorderFactory.createLineBorder(red));
             this.label.setForeground(red);
             return false;
        }
        this.textField.setText(Integer.toString(value));
        if(value < 1){
            this.textField.setBorder(BorderFactory.createLineBorder(red));
            this.label.setForeground(red);
            this.label.setText("Ακέραιος αριθμός μεγαλύτερος του 0");
            return false;
        }
        else 
            this.label.setForeground(black);
        super.stopCellEditing();
        return true;
    }

     public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
        textField.setBorder(BorderFactory.createLineBorder(black));
        this.label.setForeground(black);
        this.label.setText("Ακέραιος αριθμός μεγαλύτερος του 0");
        return super.getTableCellEditorComponent(
            table, value, isSelected, row, column);
    }

    @Override
    public void cancelCellEditing() {
         this.label.setForeground(black);
        super.cancelCellEditing(); //To change body of generated methods, choose Tools | Templates.
    }
    
}
